(function() {

  $.widget("ui.ImageUpload", {
    _create: function() {
      var div, image, path, value;
      div = $(this.element).closest("div");
      div.addClass("image-upload");
      path = Settings.UploadPath;
      value = $(this.element).val();
      image = "#";
      if (value.length) {
        image = path + value;
      }
      div.append('<img alt="аватар" src="' + image + '"/>');
      div.append('<span><input type="file" value=""/><span>Upload</span></span>');
      return div.find("span input").fileupload({
        dataType: "text",
        url: SiteMap.Shared.UploadImage,
        sequentialUploads: true,
        done: function(e, data) {
          $(this).closest(".image-upload").find("img").remove();
          $(this).closest(".image-upload").append('<img alt="avatar" src="' + path + data.result + '" />');
          return $(this).closest(".image-upload").find("input[type=hidden]").val(data.result).closest("fieldset>div").trigger("validate");
        }
      });
    }
  });

}).call(this);
